<template>
  <div class="settlement">
    <view @click="addAdress">
<view v-if="Object.keys(adressData).length > 0" class="adress">
<view>
  <uv-icon size="40rpx" name="map"></uv-icon>
</view>
 <view class="adress-text">
   <view class="name_num">
    <view>{{adressData.name}}</view>
    &nbsp;&nbsp;
    <view>{{adressData.phone}}</view>
   </view>

   <view class="adress-text_adress">
   <label> 收货地址：{{adressData.address}}{{adressData.moreAddres}}</label>
   <span class="spna_box">默认</span>
   </view>

 </view>

 <view style="position: absolute;right: 20rpx;top: 50%" class="icon_right">
  <uv-icon size="20rpx" name="arrow-right"></uv-icon>
</view>
</view>

<view v-else class="adress">
 
  <view class="adress-text">
   

   <view  style="display: flex;align-items: center;">
    <view>
  <uv-icon size="40rpx" name="map"></uv-icon>
 
</view>
&nbsp;

   <label class="text_none_adreess"> 暂无收获地址，点击添加</label>
  
   </view>
   
 </view>
 <view  class="icon_right">
  <uv-icon size="20rpx" name="arrow-right"></uv-icon>
</view>
</view>
</view>

<view>
<uv-gap height="12rpx" bgColor="#fff"></uv-gap>

<view  class="buy_goods">
<view class="title">所购商品</view>

<view v-for="(item,index) in selData.goodsList" :key="index" class="goods_list_detail">
<view class="img_left">
  <uv-image radius="30rpx" :src="item.imgUrl"  mode="heightFix" height="156rpx"></uv-image>
</view>

<view class="goods_right">
  <view class="goods_name_title">{{ item.goodsTitle }}</view>
   <view  class="cp_box">规格：{{ item.goodsSpecification || '无规格'}}</view>
   <view class="price_title">
    <view class="price_text">￥{{item.goodsPrice  }}</view>
    <view class="price_text_num">× {{item.num  }}</view>
   </view>
</view>
</view>



</view>



<uv-gap height="12rpx" bgColor="#fff"></uv-gap>

<view class="cell">

  <view class="cell_item1">
    <view>
      商品金额
    </view>
    <view>
      ￥{{ selData.totalPrice }}
    </view>
  </view>
  <view class="cell_item2">
    <view>
      运费（总重量：0.65Kg）
    </view>
    <view class="color_red">
      ￥{{ selData.freight }}
    </view>
  </view>
</view>
<uv-gap height="12rpx" bgColor="#fff"></uv-gap>
</view>

  </div>



  <view class="botton_bottom">
 <view class="hj_text">
<label >合计：</label>
<label class="color_red">¥{{ selData.totalPrice }}</label>
 </view>
    
 <view @click="gotoPay" class="button">提交订单</view>
  </view>
</template>

<script setup>
import { onShow } from '@dcloudio/uni-app';
import {getAdress,SettlementData} from '../https/api/settlement'
import { ref } from 'vue';
import {confirm} from '../../src/https/api/shopCar'

let adressData = ref({})
let getAdressData = async(data) => {
  let res = await getAdress(data)
  if(res){
    adressData.value = res.data.find(v=>v.isCheck)
  }
}

let selData = ref({})
let getSelData = async(data) => {
 let res = await  SettlementData()
 if(res){
  selData.value = res.data
 }
}



onShow(async()=>{
  let objs = JSON.parse(uni.getStorageSync('orderInfoDetail'))
  
  let res=  await confirm(objs)
  let obj = res.data 
  selData.value = {
    ...obj,
    totalPrice:obj.actualTotal,
    freight:obj.orderReduce,
    goodsList:obj.shopCartOrders[0].shopCartItemDiscounts[0].shopCartItems.map(v=>{
      return {
        ...v,
        goodsTitle:v.prodName,
        goodsSpecification:v.skuName,
        goodsPrice:v.price,
        num:v.prodCount,
        imgUrl:v.pic
      }
    })
  }

  adressData.value = {
    ...obj.userAddr,
    name:obj.userAddr.receiver,
    phone:obj.userAddr.mobile,
    address:`${obj.userAddr.province}${obj.userAddr.city}${obj.userAddr.area}`,
    moreAddres:obj.userAddr.addr,

  }
  console.log(selData.value)
  
  // getSelData({})
  // getAdressData({})
})




let doPay =(orderNumbers) =>{

  
				uni.showLoading({
					title: "请稍候",
					mask: true
				});
				
				uni.request({
					url: 'https://www.gjlfb.cn/mallApi/' + 'p/order/pay',
					method: "POST",
					header: {
						"X-Auth-Token": uni.getStorageSync('openId'),
            'Authorization': uni.getStorageSync('Token'),
						'content-type': 'application/json',
					},
					data: {
						orderNumbers: orderNumbers,
            payType: 1
					},
					success: (res) => {
						uni.hideLoading();
						console.log(JSON.stringify(res.data));
						if (res.data.code == "00000") {

							uni.requestPayment({
								provider: "wxpay",
								orderInfo: "",
								timeStamp: res.data.data.timeStamp,
								nonceStr: res.data.data.nonceStr,
								package: res.data.data.package,
								signType: res.data.data.signType,
								paySign: res.data.data.sign,
								success: (res) => {
									console.log('success:' + JSON.stringify(res));
									//支付成功
                    uni.navigateTo({
    url:'/pages/pay'
  })
								},
								fail: (err) => {
									console.log('fail:' + JSON.stringify(err));
									uni.showToast({
										title: "支付取消",
										icon: "none"
									});
								}
							})
						} else {
							uni.showToast({
								title: res.data.extMessage,
								icon: "none"
							});
						}
					},
					fail: () => {
						uni.hideLoading();
					}
				})
			}
		


let orderNumbers = ref('')
let gotoPay = () => {

  if(Object.keys(adressData.value).length>0){
  

  uni.request({
    url: 'https://www.gjlfb.cn/mallApi/p/order/submit',
    method: 'POST',
    header: {
      'content-type': 'application/json',
      'Authorization': uni.getStorageSync('Token')
    },
    data: {
      orderShopParam: [{
        remarks: '',
        shopId: 1
      }]
    }
  })
    .then(({ data }) => {
      uni.setStorageSync('orderNumbers',data.data.orderNumbers)
      orderNumbers.value = data.data.orderNumbers
      doPay(orderNumbers.value)
    })

  }else{
    uni.showToast({
      title:'请添加收货地址',
      icon:'none'
    })
  }
 
} 

let addAdress = () => {
  uni.navigateTo({
    url:'/pages/newAdress'
  })
}
</script>

<style lang="scss" scoped>
.text_none_adreess{
  display: block;
  padding: 10rpx 0;
  font-size: 22rpx;
font-family: PingFangSC;
font-weight: 400;
color: #999393;
}
.botton_bottom{
  .button{
    display: flex;
    align-items: center;
    justify-content: center;
    background: #FE3B30;
    color: white;
    padding:30rpx 0rpx;
    width: 240rpx;
  }
  .color_red{
    color: #FF5951;
  }
  .hj_text{
    font-size: 34rpx;
font-family: Microsoft YaHei;
font-weight: 400;
color: #333333;
    padding:30rpx 20rpx;
  }
  background: white;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  width: 100%;
}
.cell{
  .color_red{
    color: #FF5951;
  }
  .cell_item2{
    margin-top: 20rpx;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 26rpx;
font-family: PingFangSC;
font-weight: 500;
color: #22262E;
    display: flex;
    align-items: center;

    justify-content: space-between;
  }
  .cell_item1{
    font-size: 26rpx;
font-family: PingFangSC;
font-weight: 500;
color: #22262E;
    display: flex;
    align-items: center;

    justify-content: space-between;
  }
  padding: 20rpx 20rpx;
}
.buy_goods{
  .price_text_num{
    font-size: 24rpx;
font-family: PingFangSC;
font-weight: 400;
color: #262424;
margin-left: 
5px;
  }
  .price_text{
    font-size: 36rpx;
font-family: HelveticaNeue;
font-weight: 500;
color: #FF5951;
  }
  .price_title{
    display: flex;
    align-items: baseline;
    margin-top: 10rpx;
  }
  .cp_box{
  
    margin-top: 10rpx;
    font-size: 22rpx;
font-family: PingFangSC;
font-weight: 400;
color: #999393;
  }
  .goods_name_title{

    font-size: 26rpx;
font-family: PingFangSC;
font-weight: 400;
color: #262424;
    word-break: break-all;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2; /* 这里是超出几行省略 */
    overflow: hidden;

  }
  .img_left{
   margin-right: 20rpx;
  }
  .goods_list_detail{
    margin-bottom: 20rpx;
    
    display: flex;

    align-items: center;
  }
  .goods_list_detail:last-of-type{
    margin-bottom: 0rpx;
  }
  padding: 30rpx 20rpx;
  .title{
    font-size: 26rpx;
font-family: PingFangSC;
font-weight: 40;
margin-bottom: 20rpx;
  }
}
.adress{
 position: relative;
  .icon_right{
    margin-left: 20rpx;
  }
  .spna_box{
    vertical-align: middle
    ;
    margin-left: 10rpx;
    padding: 5rpx 10rpx;
    font-size: 15rpx;
font-family: PingFangSC;
font-weight: 500;
color: #FFFFFF;
background: #FF5951;
border-radius: 6rpx;
  }
  .adress-text_adress{

margin-top: 5rpx;
    font-size: 28rpx;
font-family: PingFangSC;
font-weight: 400;
color: #22262E;
  }
.adress-text{
  width: 88%;
  margin-left: 10rpx;
}
  .name_num{
    font-size: 30rpx;
font-family: PingFangSC;
font-weight: 600;
color: #333333;
    display: flex;
    align-items: center;

  }
  padding: 20rpx;
  display: flex;
  align-items: center;
 
}

.settlement{
  height: 100vh;
  background: #F8F8F8;
}
</style>